﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OAuth2.Framework;
using OAParameters = OAuth2.Framework.Parameters;

namespace OAuth2.Client
{
    public class AccessToken:IToken
    {
        public AccessToken(IDictionary<string, string> source) {
            Token = (string)SafeGet(OAParameters.AccessToken, source) ?? string.Empty;
            TokenType = (string)SafeGet(OAParameters.AccessTokenType, source) ?? string.Empty;
            ExpiresIn=Convert.ToInt32(SafeGet(OAParameters.AccessTokenExpiresIn,source));
            RefreshToken = (string)SafeGet(OAParameters.RefreshToken, source) ?? string.Empty;
            if (!string.IsNullOrWhiteSpace((string)SafeGet(OAParameters.Scope, source) ?? string.Empty))
                Scope = ((string)SafeGet(OAParameters.Scope, source)).Split(' ');
            Parameters = source;
        }

        #region Private Method

        private object SafeGet(string key, IDictionary<string, string> source)
        { 
            return (source.ContainsKey(key)==null)?null: source[key];
        }

        #endregion 

        #region IToken 成员

        public string Token
        {
            get;
            set;
        }

        public string TokenType
        {
            get;
            set;
        }

        public int ExpiresIn
        {
            get;
            set;
        }

        public string RefreshToken
        {
            get;
            set;
        }

        public string[] Scope
        {
            get;
            set;
        }

        public IDictionary<string, string> Parameters
        {
            get;
            private set;
        }

        #endregion
    }
}
